﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KKU.RBSFPP.Shared.DAC;
using KKU.RBSFPP.Shared.Engines;
using WebMatrix.WebData;

namespace KKU.RBSFPP.Web.Administrator.Controllers
{
    [Authorize(Users="admin")]
    public class MemberController : Controller
    {
        private RBSFPPModelContainer db = new RBSFPPModelContainer();

        //
        // GET: /Member/

        public ActionResult Index()
        {
            var members = db.Members.Include("PrivilegeLevel");
            return View(members.ToList().Display());
        }

        //
        // GET: /Member/Details/5

        public ActionResult Details(int id = 0)
        {
            Member member = db.Members.Single(m => m.Id == id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        //
        // GET: /Member/Create

        public ActionResult Create()
        {
            ViewBag.PrivilegeLevelId = new SelectList(db.PrivilegeLevels.Where(it => it.DeleteOn.HasValue==false), "Id", "Name");
            return View();
        }

        //
        // POST: /Member/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Member member)
        {
            double pid;
            if (double.TryParse(member.PersonalId, out pid))
            {
                if (ModelState.IsValid)
                {
                    member.Create();
                    db.Members.AddObject(member);
                    db.SaveChanges();

                    WebSecurity.CreateUserAndAccount(member.Username, "123456");

                    return RedirectToAction("Index");
                }
            }
            else
            {
                ModelState.AddModelError("PersonalId", "รหัสบัตรประชาชนจะต้องเป็นตัวเลขเท่านั้");
            }
            

            ViewBag.PrivilegeLevelId = new SelectList(db.PrivilegeLevels, "Id", "Name", member.PrivilegeLevelId);
            return View(member);
        }

        //
        // GET: /Member/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Member member = db.Members.Single(m => m.Id == id);
            if (member == null)
            {
                return HttpNotFound();
            }
            ViewBag.PrivilegeLevelId = new SelectList(db.PrivilegeLevels.Where(it => it.DeleteOn.HasValue == false), "Id", "Name", member.PrivilegeLevelId);
            return View(member);
        }

        //
        // POST: /Member/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Member member)
        {
            if (ModelState.IsValid)
            {
                db.Members.Attach(member);
                db.ObjectStateManager.ChangeObjectState(member, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PrivilegeLevelId = new SelectList(db.PrivilegeLevels, "Id", "Name", member.PrivilegeLevelId);
            return View(member);
        }

        //
        // GET: /Member/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Member member = db.Members.Single(m => m.Id == id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        //
        // POST: /Member/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Member member = db.Members.Single(m => m.Id == id);
            db.Members.DeleteObject(member);
            member.Delete();
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}